package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

import com.alibaba.mobileim.IYWLoginService;
import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.YWLoginParam;
import com.alibaba.mobileim.channel.event.IWxCallback;
import com.itboye.textileexpes.adapter.KefuAdapter;
import com.itboye.textileexpes.beans.NewsKefuBeans;
import com.itboye.textileexpes.presenter.AppUpdatePresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseFragment;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.IsMobileNumPhone;

public class FragmentCustomer extends BaseFragment implements Observer {
	ExpandableListView expandlsitview;

	// AdapterCustomerService adapterCustomerService;
	KefuAdapter adapter;
	AppUpdatePresenter appUpdatePresenter;

	List<NewsKefuBeans> list = new ArrayList<NewsKefuBeans>();
	// 要申请的权限
	private String[] permissions = { Manifest.permission.WRITE_EXTERNAL_STORAGE };

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_customer;
	}

	@Override
	public void onMyActivityCreated() {

		// TODO Auto-generated method stub
		// 版本判断。当手机系统大于 23 时，才有必要去判断权限是否获取
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

			// 检查该权限是否已经获取
			// int i = ContextCompat.checkSelfPermission(getActivity(),
			// permissions[0]);
			// // 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
			// if (i != PackageManager.PERMISSION_GRANTED) {
			// // 如果没有授予该权限，就去提示用户请求
			// }
		}
		appUpdatePresenter = new AppUpdatePresenter(this);
		appUpdatePresenter.onKefu();
		onExpandGroup();
		expandlsitview.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				/*
				 * Intent intent = new Intent(getActivity(), WebActivity.class);
				 * intent.putExtra( "url",
				 * "http://api.guannan.itboye.com/public/web.php/Alibaichuan/im?uid=115&touid=114"
				 * ); startActivity(intent);
				 */
				onOpenBase(groupPosition, childPosition);
				return true;
			}
		});
		expandlsitview.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				// TODO Auto-generated method stub

				return true;
			}
		});

	}

	public void onOpenBase(int postion, int chipstion) {
		// 此实现不一定要放在Application onCreate中
		final String userid = list.get(postion).getList().get(chipstion)
				.getAlibaichuan_uid();
		System.out.println("uiduiduid" + chipstion);
		// final String userid = "tbimuser_8f85517967795eeef66c225f7883bdcb";

		// 玩儿玩儿
		// 此对象获取到后，保存为全局对象，供APP使用
		// 此对象跟用户相关，如果切换了用户，需要重新获取
		final YWIMKit mIMKit = YWAPI.getIMKitInstance(userid,
				IsMobileNumPhone.BauChuanKey);
		// 开始登录
		String password = IsMobileNumPhone.isPasswrd();
		IYWLoginService loginService = mIMKit.getLoginService();
		YWLoginParam loginParam = YWLoginParam.createLoginParam(userid,
				password);
		loginService.login(loginParam, new IWxCallback() {

			@Override
			public void onSuccess(Object... arg0) {
				@SuppressWarnings("deprecation")
				Intent intent = mIMKit.getChattingActivityIntent(userid);// 获取会话列表
				startActivity(intent);

			}

			@Override
			public void onProgress(int arg0) {
				// TODO Auto-generated method stub
				System.out.println(arg0 + ">>yw");

			}

			@Override
			public void onError(int errCode, String description) {
				System.out.println(">>>>>>>>>>>>>>>>" + errCode + "\n"
						+ description);
			}
		});

	}

	public void onExpandGroup() {

		expandlsitview.setGroupIndicator(null);
		// 默认全部展开
		for (int i = 0; i < list.size(); i++) {
			expandlsitview.expandGroup(i);
			System.out.println(">>>>>>>>>>>>>>>执行了");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == appUpdatePresenter.KEfu_success) {

				list = (List<NewsKefuBeans>) entity.getData();
				adapter = new KefuAdapter(list, getActivity());
				expandlsitview.setAdapter(adapter);
				onExpandGroup();

			}
		}
	}

}
